﻿package src.com.online.rpg
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	
	/*
	 GameObject
	 ----------
	 Base class for all game objects.
	 Contains common behavior for depth sorting, collison, etc.
	 */ 
	public class GameObject extends EventDispatcher
	{
		// The actual art asset for this object -- note the use of composition instead of inheritance
		protected var _graphics:DisplayObject;
		
		private var _collisionRadius:Number = 0;		// collision sphere radius
		private var _depth:Number 			= 0;		// depth
		
		public function GameObject(graphics:DisplayObject)
		{
			if (graphics)
			{
				_graphics = graphics;
			}
			else
			{
				_graphics = new Sprite();
			}

			_collisionRadius = _graphics.width * .3;
		}
		
		// Forward these properties to the DisplayObject. Can also do this for other properties as necessary
		public function get x():Number					{ return _graphics.x; }
		public function set x(n:Number):void			{ _graphics.x = n; }
		
		public function get y():Number					{ return _graphics.y; }
		public function set y(n:Number):void			{ _graphics.y = n; }
		
		// depth property for depth sorting
		// the getters and setters could be used to signal the parent to sort only when a depth has changed
		public function get depth():Number				{ return _depth; }
		public function set depth(n:Number):void		{ _depth = n; }
		
		public function get graphics():DisplayObject	{ return _graphics; }
				
		// abstract -- subclasses override to provide their update behavior
		public function update(dt:Number):void
		{
			
		}
		
		// collision
		public function hitTest(object:GameObject):Boolean
		{
			var dx:Number = x - object.x;
			var dy:Number = y - object.y;
			var sumRadii:Number = _collisionRadius + object._collisionRadius;
			
			// if distance from one object to another is less than sum of collision radius, then collide
			return dx * dx + dy * dy <= sumRadii * sumRadii;
		}
		
	}

}